//
//  YZBaseRefreshTableView.m
//  VET
//
//  Created by YZ on 2018/7/23.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZBaseRefreshTableView.h"
#import <MJRefresh/MJRefresh.h>
//#import <SVPullToRefresh/SVPullToRefresh.h>

@implementation YZBaseRefreshTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
        self.showsVerticalScrollIndicator = NO;
//        [self addPullToRefreshWithActionHandler:^{
//            if (weakself.headerRefreshBlock) {
//                weakself.headerRefreshBlock();
//            }
//        }];
//
//        [self addInfiniteScrollingWithActionHandler:^{
//            if (weakself.footerRefreshBlock) {
//                weakself.footerRefreshBlock();
//            }
//        }];
//
//        [self.pullToRefreshView setTitle:@"下拉刷新" forState:SVPullToRefreshStateStopped];
//        [self.pullToRefreshView setTitle:@"释放开始刷新" forState:SVPullToRefreshStateTriggered];
//        [self.pullToRefreshView setTitle:@"加载中..." forState:SVPullToRefreshStateLoading];
    }
    return self;
}

- (void)headerBeginRefresh
{
//    [self triggerPullToRefresh];
    [self.mj_header beginRefreshing];
}

- (void)headerEndRefresh
{
//    [self.pullToRefreshView stopAnimating];
    [self.mj_header endRefreshing];
}

- (void)footerBeginRefresh
{

}

- (void)footerEndRefresh
{
//    [self.infiniteScrollingView stopAnimating];
    [self.mj_footer endRefreshing];
}

- (void)showNoMoreData
{
//    UILabel *lable = [UILabel new];
//    lable.frame = CGRectMake(0, 0, kScreenWidth, 40);
//    lable.font = kCurrentDeviceFont(14);
//    lable.textAlignment = NSTextAlignmentCenter;
//    lable.text = @"没有更多数据";
//    [self.infiniteScrollingView setCustomView:lable forState:SVPullToRefreshStateAll];
    self.mj_footer.state = MJRefreshStateNoMoreData;
}

- (void)resetState
{
    self.mj_footer.state = MJRefreshStateIdle;
}


- (void)setFooterRefreshBlock:(YZFooterRefreshBlock)footerRefreshBlock
{
    _footerRefreshBlock = footerRefreshBlock;
    YZWeakSelf(self);
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (weakself.footerRefreshBlock) {
            weakself.footerRefreshBlock();
        }
    }];
}

- (void)setHeaderRefreshBlock:(YZHeaderRefreshBlock)headerRefreshBlock
{
    _headerRefreshBlock = headerRefreshBlock;
    YZWeakSelf(self);
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (weakself.headerRefreshBlock) {
            weakself.headerRefreshBlock();
        }
    }];
}
@end
